/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package projetocompiladores;

import java.awt.geom.AffineTransform;
import java.awt.geom.Point2D;
import java.util.ArrayList;
import org.jhotdraw.draw.AttributeKeys;
import org.jhotdraw.draw.BezierFigure;
import org.jhotdraw.draw.GraphicalCompositeFigure;
import org.jhotdraw.draw.TextFigure;
import org.jhotdraw.draw.layouter.LocatorLayouter;
import org.jhotdraw.geom.BezierPath.Node;

/**
 *
 * @author Anderson
 */
public class LabeledBezierConnectionFigure extends GraphicalCompositeFigure {

    private ArrayList<Point2D.Double> bezierPoints;
    private BezierFigure bezierFigure;
    private TextFigure labelFigure;

    public enum Position {
        DIREITA,
        ESQUERDA,
        CENTER
    };

    private Position position;

    public LabeledBezierConnectionFigure() {
        bezierPoints = new ArrayList<Point2D.Double>(2);
        bezierPoints.add(null); bezierPoints.add(null);
        setLayouter(new LocatorLayouter());
        setLabel("");
        position = Position.CENTER; // posicao default do label
    }
    
    public void setLabel(String text) {
        labelFigure = new TextFigure(text);
    }

    public void setLabelPosition(Position position) {
        this.position = position;
    }

    private void adjustLabelPosition(Position position) {
        if(position == Position.DIREITA) {
            double figureInicioX = bezierPoints.get(0).getX();
            double labelTamanho = labelFigure.getBounds().width;

            double figureInicioY = bezierPoints.get(0).getY();

            labelFigure.transform(new AffineTransform(0, 0, 0, 0, figureInicioX + 10,
                                                                  figureInicioY - 15));

        } else if(position == Position.ESQUERDA) {
            double figureInicioX = bezierPoints.get(0).getX();
            double labelTamanho = labelFigure.getBounds().width;

            double figureInicioY = bezierPoints.get(0).getY();

            labelFigure.transform(new AffineTransform(0, 0, 0, 0, figureInicioX - labelTamanho -10,
                                                                  figureInicioY - 15));
        } else if(position == Position.CENTER) {
            double centerFigureX = this.getBounds().getCenterX();
            double centerLabelX = labelFigure.getBounds().getCenterX();

            labelFigure.transform(new AffineTransform(0, 0, 0, 0,
                    centerFigureX - centerLabelX,
                    this.getBounds().getCenterY() - labelFigure.getBounds().getCenterY()));
        }
    }

    public void setStartConnectorPosition(Point2D.Double posicao) {
        bezierPoints.set(0, posicao);
    }

    public void setEndConnectorPosition(Point2D.Double posicao) {
        bezierPoints.set(bezierPoints.size()-1, posicao);
    }

    public void addNode(Point2D.Double ponto) {
        Point2D.Double temp = bezierPoints.remove(bezierPoints.size()-1);
        bezierPoints.add(ponto);
        bezierPoints.add(temp);
    }

    public void draw() throws Exception {
        if(bezierPoints.get(0) == null || bezierPoints.get(1) == null) {
            throw new Exception("Nao eh possivel desenhar a linha se os pontos inicial e final nao forem inicializados!");
        }
        
        bezierFigure = new BezierFigure();
        for(Point2D.Double ponto : bezierPoints) {
            bezierFigure.addNode(new Node(ponto));
        }
        bezierFigure.set(AttributeKeys.STROKE_WIDTH, 2.0);
        setPresentationFigure(bezierFigure);

//        labelFigure.set(LocatorLayouter.LAYOUT_LOCATOR, new BezierLabelLocator(0, -Math.PI / 2, 4));

//        double centerFigureX = this.getBounds().getCenterX();
//        double centerLabelX = labelFigure.getBounds().getCenterX();
//
//        labelFigure.transform(new AffineTransform(0, 0, 0, 0, centerFigureX - centerLabelX, this.getBounds().getCenterY() - labelFigure.getBounds().getCenterY()));

        adjustLabelPosition(position);
        
        add(labelFigure);
    }
}
